package com.neusoft.chenhanhua.ChineseChess;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.w3c.dom.CDATASection;


public class Checker extends JFrame  {
	public static void main(String[] args) {
		Checker jf = new Checker();
		//设置窗口的大小
		jf.setSize(400, 500);
		//设置标题
		jf.setTitle("中国象棋");
		//关闭时同时关闭虚拟机
		jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
		GamePanel p = new GamePanel();
		//添加窗口中
		jf.add(p);
		
		//显示窗口，建议放在最后一行
		jf.setVisible(true);
	}
	public static class GamePanel extends JPanel{
		int qq=0;
		int q=0;
		 int x1=0;
		 int y1=0;
		 int x2 =0;
		 int y2=0;
		String[] names= {"che","ma","xiang","shi","boss","shi","xiang","ma","che","pao","pao","bing","bing","bing","bing","bing","che","ma","xiang","shi","boss","shi","xiang","ma","che","pao","pao","bing","bing","bing","bing","bing"};
		int [] xs= {1,2,3,4,5,6,7,8,9,2,8,1,3,5,7,9,1,2,3,4,5,6,7,8,9,2,8,1,3,5,7,9};
		int [] ys= {1,1,1,1,1,1,1,1,1,3,3,4,4,4,4,4,10,10,10,10,10,10,10,10,10,8,8,7,7,7,7,7};
		public GamePanel() {
			addMouseListener(ma);
			
		}
		 private MouseAdapter ma=new MouseAdapter() {
			 
			 public void mousePressed(MouseEvent e) {
				
				 if (q==0) {
					 x1 = e.getX();
					 y1 = e.getY();
					 //计算第一次点击的坐标
					 x1=(int)Math.floor(((x1-5)/40))+1;
					 y1=(int)Math.floor(((y1-5)/40))+1;
					 q+=1;
				} else {
					 x2 = e.getX();
					 y2 = e.getY();
					 //计算第二次点击的坐标
					 x2=(int)Math.floor(((x2-5)/40))+1;
					 y2=(int)Math.floor(((y2-5)/40))+1;
					 for (int i = 0; i < xs.length; i++) {
						if (xs[i] == x1 && ys[i] == y1) {
							//判断第一次点的是什么，然后是否符合规则，如果符合就可以走
							ChessRule cr=new ChessRule();
							//行走规则
							boolean re=cr.zhoux(x1,y1,x2,y2,i, names,xs,ys);
							//吃子
							for (int j = 0; j < names.length; j++) {
								//根据x2和y2判断是否有棋子,(i<16&&j>=16),(i>16&&j<16)这个是判断敌我
								if (   names[i]!=names[26]&& names[i]!=names[25]&&names[i]!=names[10]&&names[i]!=names[9]&&xs[j]==x2&&ys[j]==y2&&((i<16&&j>=16) || (i>16&&j<16))) {
									xs[j]=0;
									ys[j]=0;
									xs[i]=x2;
									ys[i]=y2;
									//再次使用paint
									repaint();
								}
								//炮的吃子，和炮的行走规则是分开的，在不满足行走规则的情况下从判断是否是吃子的规则
								if (names[i]==names[26]&& names[i]==names[25]&&names[i]==names[10]&&names[i]==names[9]&&xs[j]==x2&&ys[j]==y2&&((i<16&&j>=16) || (i>16&&j<16))) {
//									这里是判断它中间有多少个值，不包括y2=ys[j2]
									//就是在这里写
									if (x1==x2&&y1!=y2) {
										for (int j2 = 0; j2 < names.length; j2++) {
											if ((xs[j2]==x1&&y1<y2&&y1<ys[j2]&&y2>ys[j2]) || (xs[j2]==x1&&y1>y2&&y1>ys[j2]&&ys[j2]>y2)) {
												qq+=1;
												System.out.println("直行");
											}
										}
									}
									if (y1==y2&&x1!=x2) {
										for (int j2 = 0; j2 < names.length; j2++) {
											if (ys[j2]==y1&&x2>x1&&xs[j2]>x1&&x2>xs[j2] || (ys[j2]==y1&&x1>x2&&x1>xs[j2]&&xs[j2]>x2) ) {
												qq+=1;
												System.out.println("横行");
											}
										}
									}
									if (qq==1) {
										xs[j]=0;
										ys[j]=0;
										xs[i]=x2;
										ys[i]=y2;
										qq=0;
										//再次使用paint
										repaint();
										
									}else {
										//这是没有的话也把qq变为0
										qq=0;
									}
								}
							}
							//符合行走规则就重新画
							if (re) {
								xs[i]=x2;
								ys[i]=y2;
								//再次使用paint
								repaint();
							}
						}
					}
					 q-=1;
				}
				}
		 };
		//画棋盘
		@Override//注解，表示该方法是一个重写方法，不是方法重写不可以加
		public void paint(Graphics g) {
			super.paint(g);
			//棋盘
	        Image bg = Toolkit.getDefaultToolkit().getImage("pic/qipan.jpg");
			g.drawImage(bg, 0, 0, this);
			for(int i = 0; i < names.length; i++) {
				if (i<16) {
					Chess c = new Chess(names[i], new Point(xs[i], ys[i]), 0);
					c.draw(g, this);
				} else {
					Chess c = new Chess(names[i], new Point(xs[i], ys[i]), 1);
					c.draw(g, this);
				}
			}
		}
	}
}
